package cn.jdy.common.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.AjaxUtilCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.safe.EncryptUtil;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.HttpUtil;
import cn.jdy.framework.util.JsonUtil;

/**
 * 短信发送工具类
 * 
 * @author zhaigt
 * @date 2015年3月7日
 */
public class SMSUtil {
  
  // yuntongxun
  public static final String acountName = "aaf98f894a038dc2014a0928a2ff0403";//accountSid
  public static final String acountToken = "c59b9a7716fd483faa8b2c652e243b7f";//
  public static final String appId = "aaf98f894a146931014a147cdcd90007";//
  public static final String templateId = "14877";//"8561";//短信模版
  // yuntongxun
  
  public static void main(String[] args) {
    try {
      String str = yuntongxun("18671085238","18883","10");
      System.out.println(str);
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  /**
   * 短信平台@ yuntongxun.com
   * http://docs.yuntongxun.com/
   * @param args
   * @throws NoSuchAlgorithmException
   * @throws UnsupportedEncodingException
   */
  public static String yuntongxun(String phone,String code,String limit) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
    EncryptUtil eu = new EncryptUtil();
    
    String timestamp = DateUtil.getCurrentDate4();
    String sig = acountName + acountToken + timestamp;
    String signature = eu.md5Digest(sig);
    
    StringBuilder url = new StringBuilder("");
    url.append("https://app.cloopen.com:8883/2013-12-26/Accounts/");
    url.append(acountName+"/");// accountSid
    url.append("SMS/");// func
    url.append("TemplateSMS?");// funcdes
    url.append("sig="+signature);// SigParameter
    
    // 拼接请求参数
//    String json = "{'to':'18671085238','appId':'aaf98f894a146931014a147cdcd90007','templateId':'8561','datas':['300323','5']}";
    Map<String, Object> jsonMap = new HashMap<String, Object>();
    jsonMap.put("to", phone);
    jsonMap.put("appId", appId);
    jsonMap.put("templateId", templateId);
    jsonMap.put("datas", new String[]{code,limit});
    
    // 发送请求
    HashMap<String, String> header = new HashMap<String, String>();
    header.put("Accept", "application/json");
    header.put("Content-Type", "application/json;charset=utf-8");
    
    String res=null;
    try {
      String src = acountName + ":" + timestamp;
      String auth = eu.base64Encoder(src);
      header.put("Authorization", auth);//  加密 鉴权
      
      String json = JsonUtil.toJson(jsonMap);
      res = HttpUtil.sendByUserAgent(url.toString(), "utf-8", header, json);
      
      BaseLog.d(SMSUtil.class, "请求参数："+json);
      BaseLog.d(SMSUtil.class, "返回结果："+res);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return res;
  }
  
  /**
   * 短信平台@ www.10690071.com
   * @param phonecode
   * @throws IOException
   */
  public static String sms10690071(String phonecode,String code) throws IOException{
      String url = "";// 短信平台发送接口地址
      String msgtemple = "";// 短信模版
      String param = "";
      
      url = "http://www.10690071.com:9007/url_sms/struts/urlCurrentAction!toCurrentSms";
      param = "userName=admin&compCode=czzz&pwd=47624E748EF00A60F06202A5610B17E4";
      msgtemple = "您好，您的验证码是%s.";
  
      StringBuffer sb = new StringBuffer(param);
      String msg = String.format(msgtemple, code);// 替换短信内容
  
      sb.append("&phone=");
      sb.append(phonecode);
      sb.append("&message=");
      sb.append(URLEncoder.encode(msg, "UTF-8"));
     
      String xml = HttpUtil.sendGet(url + "?" + sb.toString(), "utf-8");
      BaseLog.d(AjaxUtilCtrl.class, xml);
      return xml;
    }
}
